const downloadService = {
  downloadFile: (params) => {
    const { mainWindow } = params;
    console.log("start");
    //console.log(mainWindow.webContents);
    const filelist = [
      "http://tools.nxycdl.com/fabric-ca/hyperledger-fabric-darwin-amd64-2.1.1.tar.gz",
      "http://tools.nxycdl.com/git/git-2.27.0.tar.gz",
      "http://tools.nxycdl.com/nvm/install.sh",
    ];
    filelist.map((item) => {
      console.log("开始下载");
      mainWindow.webContents.downloadURL(item);
    });
    // mainWindow.webContents.downloadURL(
    //   "http://tools.nxycdl.com/fabric-ca/hyperledger-fabric-darwin-amd64-2.1.1.tar.gz"
    // );

    mainWindow.webContents.session.on(
      "will-download",
      (event, item, webContents) => {
        item.setSavePath(process.cwd() + "/" + item.getFilename());

        console.log("y", process.cwd(), item.get);
        //console.log(item);
        // console.log(event);
        // console.log(item);
        // console.log(webContents);
        item.on("updated", (event, state) => {
          if (state === "interrupted") {
            console.log("Download is interrupted but can be resumed");
          } else if (state === "progressing") {
            if (item.isPaused()) {
              console.log("Download is paused");
            } else {
              console.log(`Received bytes: ${item.getReceivedBytes()}`);
            }
          }
        });
        item.once("done", (event, state) => {
          if (state === "completed") {
            console.log("Download successfully");
          } else {
            console.log(`Download failed: ${state}`);
          }
        });
      }
    );
  },
};

module.exports = downloadService;
